export function request(options) {
    return new Promise((resolve, reject) => {
        const { url, method, body, param } = options;
        const xhr = new XMLHttpRequest();
        xhr.open(method, url);
        xhr.onreadystatechange = function(e) {
            if (xhr.readyState === 4 && xhr.status ===200) {
                if (xhr.getResponseHeader('Content-Type').indexOf('application/json') !== -1) {
                    resolve(JSON.parse(xhr.response));
                } else {
                    resolve(xhr.response);
                }
            }
        }
        xhr.onerror = function(err) {
            reject(err);
        }

        if (body) {
            xhr.setRequestHeader('Content-type', 'application/json')
        }

        xhr.send(JSON.stringify(body));
    })
}

